package com.huawei.petSystemdemo.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ImgRepository {

    public static final Map<String, Bitmap> imgBuff = new HashMap<>();
    public static void getImage(String url, Consumer<Bitmap> callback) {
        Bitmap bitmap2 = imgBuff.get(url);
        if (bitmap2 != null)
        {
            callback.accept(bitmap2);
            return;
        }

        new MyRequest<Bitmap>().getStream(url, null, response -> {
            InputStream is = response.body().byteStream();
            Bitmap bitmap = BitmapFactory.decodeStream(is);
            imgBuff.put(url, bitmap);
            return bitmap;
        }, callback);
    }
}




